package com.irdstudio.tdp.executor.core.plugin.migrate.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移字段映射信息类-[表名: 迁移字段映射信息(migrate_field_mapping)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-04
 */
public class MigrateFieldMappingDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateFieldMappingDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateFieldMapping> queryMigrateFieldMappingWithAll()
			throws SQLException {
		List<MigrateFieldMapping> dcList = new ArrayList<MigrateFieldMapping>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		MigrateFieldMapping dc = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM migrate_field_mapping");
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new MigrateFieldMapping();
				dc.setFieldMappingId(rs.getString("field_mapping_id"));
				dc.setTableMappingId(rs.getString("table_mapping_id"));
				dc.setSourceFieldCode(rs.getString("source_field_code"));
				dc.setSourceFieldName(rs.getString("source_field_name"));
				dc.setFieldMappingRule(rs.getString("field_mapping_rule"));
				dc.setTargetFieldCode(rs.getString("target_field_code"));
				dc.setTargetFieldName(rs.getString("target_field_name"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateFieldMappingWithAll is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateFieldMapping> queryMigrateFieldMappingWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<MigrateFieldMapping> dcList = new ArrayList<MigrateFieldMapping>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM migrate_field_mapping "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				MigrateFieldMapping dc = new MigrateFieldMapping();
				dc.setFieldMappingId(rs.getString("field_mapping_id"));
				dc.setTableMappingId(rs.getString("table_mapping_id"));
				dc.setSourceFieldCode(rs.getString("source_field_code"));
				dc.setSourceFieldName(rs.getString("source_field_name"));
				dc.setFieldMappingRule(rs.getString("field_mapping_rule"));
				dc.setTargetFieldCode(rs.getString("target_field_code"));
				dc.setTargetFieldName(rs.getString("target_field_name"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateFieldMappingWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
